//
// Created by 202207384 on 2022/9/28.
//

#ifndef DEMO_DAT_PARSER_H
#define DEMO_DAT_PARSER_H

#include <string>
#include <fstream>
#include <vector>

#include "data_structure.h"

using namespace std;

/**
 * dat文件解析
 */
class DatParser
{
public:
#pragma pack(1)
    struct FileHead
    {
        float version;              //版本号
        size_t protoContentSize{0}; // proto内容数据大小
        uint16_t commentLength{0};  //评论数据大小
        char reserve[200];          //保留字段
    };
#pragma pack()

#pragma pack(1)
    struct RawDataHead
    {
        char name[100];   // topic name
        size_t size;      // dat的原始数据大小
        double timestamp; //时间戳
    };
#pragma pack()

    struct ReplayData
    {
        RawDataHead head;
        std::string data; //保存在dat的原始数据
    };

public:
    DatParser();

    ~DatParser();

    /**
     *解析dat
     * @param filePath dat文件路径
     */
    void parse(const string &filePath, std::vector<DatParser::ReplayData> &repVec);

    void parse_forEdc(const string &filePath, SocketData &sdh32, SocketData &sdh16, SocketData &sdh8, 
                                                        SocketData &sdh4, SocketData &sdh2, SocketData &sdh1);

private:
    /**
     * 读取dat文件内容
     * @param ifs dat文件输入流
     * @param repVec 结构集
     */
    void doParse(ifstream &ifs, std::vector<ReplayData> &repVec);

    void doParse_forEdc(ifstream &ifs, SocketData &sdh32, SocketData &sdh16, SocketData &sdh8, 
                                                SocketData &sdh4, SocketData &sdh2, SocketData &sdh1);

};

#endif // DEMO_DAT_PARSER_H
